//
//  RJOutLine.m
//  AirQuality
//
//  Created by Po on 15/7/3.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJOutLine.h"
#import "SHLineGraphView.h"
#import "SHPlot.h"
@interface RJOutLine ()

@property (retain, nonatomic) SHLineGraphView * lineView;
@property (retain, nonatomic) NSString * keyString;             //具体空气数据
@property (retain, nonatomic) NSArray * airData;                //每小时平均值
@property (retain, nonatomic) NSArray * timeArray;              //最近24小时
@property (assign, nonatomic) CGFloat maxHeight;

- (void)initInterface;
@end
@implementation RJOutLine

- (instancetype)initWithFrame:(CGRect)frame data:(NSDictionary *)data maxHeight:(CGFloat)height
{
    self = [super initWithFrame:frame];
    if (self) {
        if ([data count] != 0) {
            _keyString = [data[@"data"] allKeys][0];
            _airData = [NSArray arrayWithArray:[data[@"data"] allValues][0]];
            _timeArray = [NSArray arrayWithArray:data[@"times"]];
            _maxHeight = height;
            
        }
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    if (_keyString) {
        [self setContentSize:CGSizeMake(1000, self.bounds.size.height)];
        [self setContentOffset:CGPointMake(0, 0)];
        [self addSubview:[self getLineView]];
        [_lineView setupTheView];
    } else {
        UILabel * label = [[UILabel alloc] initWithFrame:self.bounds];
        [label setText:@"没有获取到折线图数据"];
        [label setFont:[UIFont boldSystemFontOfSize:16]];
        [label setTextColor:[UIColor whiteColor]];
        [label setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:label];
    }
}

- (SHLineGraphView *)getLineView
{
    if (!_lineView) {
        CGRect rect = CGRectZero;
        rect.size = self.contentSize;
        _lineView = [[SHLineGraphView alloc] initWithFrame:rect];
        _lineView.barChartShowNum = NO;
        _lineView.fillLineRect = YES;
        _lineView.AirType = [RJTools getTrueTypeTitlleWithCellKey:_keyString];
        if ([_lineView.AirType isEqualToString:@"PM2.5"]) {
            _lineView.AirType = @"PM2_5";
        }
        
        [self initLineView];
    }
    return _lineView;
}

/**初始化折线图*/
- (void)initLineView
{
//    _lineView.barChartShowBtn = NO;
//    _lineView.barChartShowNum = YES;
//    //线条下方填充
//    _lineView.fillLineRect = YES;
    //可以设置是否显示柱状图
    _lineView.barChartAlpha = 0;
    _lineView.checkFillColor = YES;
    _lineView.fillColor = [UIColor clearColor];
    NSDictionary *_themeAttributes = @{
                                       kXAxisLabelColorKey : GetRGBColor(85, 85, 85, 1),
                                       kXAxisLabelFontKey : [UIFont fontWithName:@"TrebuchetMS" size:12],
                                       kYAxisLabelColorKey : GetRGBColor(85, 85, 85, 1),
                                       kYAxisLabelFontKey : [UIFont fontWithName:@"TrebuchetMS" size:12],
                                       kYAxisLabelSideMarginsKey : @10,
                                       kPlotBackgroundLineColorKye : GetRGBColor(85, 85, 85, 0.2)
                                       };
    _lineView.themeAttributes = _themeAttributes;
    _lineView.yAxisRange = @(_maxHeight);
    
    
    
    _lineView.yAxisSuffix = @"ug";
    NSMutableArray * xLabelArray = [[NSMutableArray alloc] init];
    for (NSUInteger i = 1; i <= 24 ; i ++) {
        if (_timeArray.count == 0) {
            return;
        }
        [xLabelArray addObject:@{@(i):_timeArray[i -1]}];
    }
    _lineView.xAxisValues = xLabelArray;
    SHPlot *_plot1 = [[SHPlot alloc] init];
    //转化数据
    NSMutableArray * dataArray = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0; i < 24; i ++) {
        [dataArray addObject:@{@(i + 1):_airData[i]}];
    }
    _plot1.plottingValues = dataArray;
    
    NSMutableArray * labelsTitle = [NSMutableArray arrayWithCapacity:0];
    for (NSNumber * number in _airData) {
        if ([_keyString isEqualToString:@"co"]) {
            [labelsTitle addObject:[NSString stringWithFormat:@"%.2f",[number floatValue]]];
        } else {
            [labelsTitle addObject:[NSString stringWithFormat:@"%ld",(long)[number integerValue]]];
        }
    }
    _plot1.plottingPointsLabels = labelsTitle;
    
    NSDictionary *_plotThemeAttributes = @{
                                           kPlotFillColorKey : GetRGBColor(66, 195, 191, 0.5),
                                           kPlotStrokeWidthKey : @2,
                                           kPlotStrokeColorKey : SysMainColor,
                                           kPlotPointFillColorKey : SysMainColor,
                                           kPlotPointValueFontKey : [UIFont fontWithName:@"TrebuchetMS" size:18]
                                           };
    
    _plot1.plotThemeAttributes = _plotThemeAttributes;
    [_lineView addPlot:_plot1];
}


@end
